<?PHP
  class Role{
    public $name = "";
    public $ID   = -1;
	
    public function load($id){
	  $id   = $GLOBALS['db']->EscapeString($id);
	  $role = $GLOBALS['db']->ReadRow("SELECT * FROM {'dbprefix'}roles WHERE id = '".$id."'");
	  if($role){
	    $this->name = $role->name;
		$this->ID   = $role->id;
	  }
	}
	
	public function getAllRolls(){
	  $roles = $GLOBALS['db']->ReadRows("SELECT id FROM {'dbprefix'}roles ORDER BY name");
	  foreach($roles as $role){
	    $newRole = new Role();
		$newRole->load($role->id);
		$res[] = $newRole;
	  }
	  return $res;
	}
	
	public function canAccess($page){
	  $id     = $GLOBALS['db']->EscapeString($this->ID);
	  $pageid = $GLOBALS['db']->EscapeString($page->id);
	  return $GLOBALS['db']->ReadField("SELECT COUNT(*) FROM {'dbprefix'}role_rights WHERE role = '".$id."' AND page = '".$pageid."'") > 0;
	}
	
	public function clearAccess($page){
	  $pageid = $GLOBALS['db']->EscapeString($page->id);
	  return $GLOBALS['db']->Execute("DELETE FROM {'dbprefix'}role_rights WHERE page = '".$pageid."'");
	}
	
	public function allowAccess($page){
	  $id     = $GLOBALS['db']->EscapeString($this->ID);
	  $pageid = $GLOBALS['db']->EscapeString($page->id);
	  return $GLOBALS['db']->Execute("INSERT INTO {'dbprefix'}role_rights (role,page) VALUES('".$id."','".$pageid."')");
	}
		  
  }
?>